package cn.iocoder.yudao.module.blog.controller.admin.askresponse;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.askresponse.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.askresponse.BlogAskResponseDO;
import cn.iocoder.yudao.module.blog.convert.askresponse.BlogAskResponseConvert;
import cn.iocoder.yudao.module.blog.service.askresponse.BlogAskResponseService;

@Tag(name = "管理后台 - 学员问题回复")
@RestController
@RequestMapping("/blog/ask-response")
@Validated
public class BlogAskResponseController {

    @Resource
    private BlogAskResponseService askResponseService;

    @PostMapping("/create")
    @Operation(summary = "创建学员问题回复")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:create')")
    public CommonResult<Long> createAskResponse(@Valid @RequestBody BlogAskResponseCreateReqVO createReqVO) {
        return success(askResponseService.createAskResponse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学员问题回复")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:update')")
    public CommonResult<Boolean> updateAskResponse(@Valid @RequestBody BlogAskResponseUpdateReqVO updateReqVO) {
        askResponseService.updateAskResponse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学员问题回复")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:ask-response:delete')")
    public CommonResult<Boolean> deleteAskResponse(@RequestParam("id") Long id) {
        askResponseService.deleteAskResponse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学员问题回复")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:query')")
    public CommonResult<BlogAskResponseRespVO> getAskResponse(@RequestParam("id") Long id) {
        BlogAskResponseDO askResponse = askResponseService.getAskResponse(id);
        return success(BlogAskResponseConvert.INSTANCE.convert(askResponse));
    }

    @GetMapping("/list")
    @Operation(summary = "获得学员问题回复列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:query')")
    public CommonResult<List<BlogAskResponseRespVO>> getAskResponseList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogAskResponseDO> list = askResponseService.getAskResponseList(ids);
        return success(BlogAskResponseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学员问题回复分页")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:query')")
    public CommonResult<PageResult<BlogAskResponseRespVO>> getAskResponsePage(@Valid BlogAskResponsePageReqVO pageVO) {
        PageResult<BlogAskResponseDO> pageResult = askResponseService.getAskResponsePage(pageVO);
        return success(BlogAskResponseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学员问题回复 Excel")
    @PreAuthorize("@ss.hasPermission('blog:ask-response:export')")
    @OperateLog(type = EXPORT)
    public void exportAskResponseExcel(@Valid BlogAskResponseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogAskResponseDO> list = askResponseService.getAskResponseList(exportReqVO);
        // 导出 Excel
        List<BlogAskResponseExcelVO> datas = BlogAskResponseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "学员问题回复.xls", "数据", BlogAskResponseExcelVO.class, datas);
    }

}
